package com.hnkywl.sts.dao.zjgl;

import com.hnkywl.sts.entity.cwgl.Kmdm;
import com.hnkywl.sts.entity.zjgl.Fycb;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-23
 * Time: 下午1:37
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class FycbDao extends SimpleHibernateTemplate<Fycb, Long> {

//    public List<Kmdm> findKmdmByGs(Long gsId) {
//        String hql = "from Kmdm where nbgs_id = ?";
//        return find(hql, gsId);
//    }

    /**
     * 查询费用
     *
     * @param
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<Fycb> findAllFycb( String yf,String nf,String gsMc,Long Ids, int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Fycb> page = new Page<Fycb>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (nf != null && yf!=null) {
            if( !yf.equals("11") && !yf.equals("12") ){
                yf = "0"+yf.trim();
            }
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" month = ? ");
            param.add(Integer.parseInt(nf.trim()+yf.trim()));
        }
        if (Ids != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" gs.id = ? ");
            param.add(Ids);
        }
        if (Ids == null && gsMc!=null && !gsMc.equals("")) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" gs.jc like ? ");
            param.add("%" + gsMc.trim() + "%");
        }
        //==============记录数量===============
        hql = "select count(*) from Fycb where 1=1" ;
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Fycb where 1=1" ;
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    public List<Fycb> findAllFycbByDate(String date) {
        String hql = "from Fycb where month = ? ";
        return (List<Fycb>)find(hql,Integer.parseInt(date));
    }

//    public Kmdm findKmdmDetailByGs(Long gsId,String code){
//        String hql = "from Kmdm where nbgs.id = ? and code = ?";
//        return (Kmdm)find(hql,gsId,code).get(0);
//    }


//    public void createSjcode(Long gsId) {
//        String dl = "'dl'";
//        String sql = "update t_sys_kmdm" +
//                " set SJCODE=(case when length(code)!=4 then substr(code,1,length(code)-2) else " + dl + " end) where nbgs_id =" + gsId + "";
//        executeSQL(sql);
//    }

}
